<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date    2016-11-22 21:34:36 
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends Admin_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $userinfo = $this->session->userinfo;
        if(!isset($userinfo))
        {
            redirect('admin/login'); 
        }
        $this->load->model('article_model');
    }
    //文章列表
	public function index()
	{	
		$field = array('id','category_id','title','user_id','write_time','post_time','is_draft');
		$data = $this->article_model->get_data($field);

		for($i = 0;$i<count($data);$i++)
		{
			$data[$i]['write_time'] = date("Y-m-d H:i:s",$data[$i]['write_time']);
			$data[$i]['post_time'] = date("Y-m-d H:i:s",$data[$i]['post_time']);
		}
		
		$items['items'] = $data;
		$this->load->view('templates/header');
		$this->load->view('templates/menu');
		$this->load->view('article_list',$items);
	}
	//添加文章
	public function add()
	{
		$this->load->view('article_add');
	}
	//文章数据入库
	public function insert()
	{
		// id category_id title keyword user_id content write_time post_time is_draft 
		$data = $this->input->post(array('title','keyword','category_id','content'),TRUE);
		$data['user_id'] = 1;
		$data['write_time'] = time();
		$data['post_time'] = time();
		$data['is_draft'] = 1;
		// var_dump($data);
        $mytable = $this->db->dbprefix('article');
        $sql = $this->db->insert_string($mytable,$data);
		// if($this->article_model->insert($data))
		// {
		// 	$this->success('新增文章成功^_^','admin/article');
		// }
		// else
		// {
		// 	$this->success('新增文章失败!','admin/article/add');
		// }
	}
	//文章草稿
	public function draft()
	{
		$this->load->view('templates/header');
		$this->load->view('templates/menu');

		$this->load->view('article_draft');
	}
}